// pages/cart/cart.js
// var Temp = require('pages/car_details/car_details.js');
// Page(Object.assign({}, Temp.Quantity, {
const app = getApp()
var numbers = 1;
var bool = true;
Page({
  data: {
    show_edit: "block",
    edit_name: "编辑",
    edit_show: "none",
    // list: [],               // 购物车列表
    // hasList: false,          // 列表是否有数据
    // 默认展示数据
    hasList: true,
    // 金额
    totalPrice: 0, // 总价，初始为0
    // 全选状态
    selectAllStatus: false, // 全选状态，默认全选
    isAllSelect: false,
    totalMoney: 0,
    // 商品详情介绍
    minusStatus: 'disabled',
    carts: [],
  },

  /* 点击减号 */
  bindMinus: function(e) {
    var that = this;
    var i = e.currentTarget.id;
    var carts = that.data.carts;
    var num = carts[i].num;
    // 如果大于1时，才可以减  
    if (num <= 1) {
      return false;
    }
    num = num - 1;
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
   carts[i].num = num
    that.setData({
      minusStatus: minusStatus,
      carts: carts
    });
    that.count_price();
  },
  /* 点击加号 */
  bindPlus: function(e) {
    var that = this;
    var i = e.currentTarget.id;
    var carts = that.data.carts;
    var num = parseInt(carts[i].num);
    // 不作过多考虑自增1  
    num = num + 1;
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num < 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    carts[i].num = num
    that.setData({
      minusStatus: minusStatus,
      carts: carts
    });
    that.count_price();
  },

  btn_edit: function () {
    var that = this;
    if (bool) {
      that.setData({
        edit_show: "block",
        edit_name: "取消",
        show_edit: "none"
      })
      bool = false;
    } else {
      that.setData({
        edit_show: "none",
        edit_name: "编辑",
        show_edit: "block"
      })
      bool = true;
    }
  },

  selectList(e) {
    var that = this;
    // 获取选中的radio索引
    var index = e.currentTarget.dataset.index;
    // 获取到商品列表数据
    var list = that.data.carts;
    // 默认全选
    that.data.selectAllStatus = true;
    // 循环数组数据，判断----选中/未选中[selected]
    list[index].isSelect = !list[index].isSelect;
    // 如果数组数据全部为selected[true],全选
    for (var i = list.length - 1; i >= 0; i--) {
      if (!list[i].isSelect) {
        that.data.selectAllStatus = false;
        break;
      }
    }
    // 重新渲染数据
    that.setData({
      carts: list,
      selectAllStatus: that.data.selectAllStatus
    })
    // 调用计算金额方法
    that.count_price();
  },
  
  /**
   * 购物车全选事件
   */
  selectAll(e) {
    // 全选ICON默认选中
    let selectAllStatus = this.data.selectAllStatus;
    // true  -----   false
    selectAllStatus = !selectAllStatus;
    // 获取商品数据
    let list = this.data.carts;
    // 循环遍历判断列表中的数据是否选中
    for (let i = 0; i < list.length; i++) {
      list[i].isSelect = selectAllStatus;
    }
    // 页面重新渲染
    this.setData({
      selectAllStatus: selectAllStatus,
      carts: list
    });
    // 计算金额方法
    this.count_price();
  },
  // 计算价格
  count_price() {
    // 获取商品列表数据
    let list = this.data.carts;
    // 声明一个变量接收数组列表price
    let total = 0;
    // 循环列表得到每个数据
    for (let i = 0; i < list.length; i++) {
      // 判断选中计算价格
      if (list[i].isSelect) {
        // 所有价格加起来 count_money
        total += list[i].num * list[i].price;
      }
    }
    // 最后赋值到data中渲染到页面
    this.setData({
      carts: list,
      totalPrice: total.toFixed(2)
    });
  },
  
  // 删除功能
  delete_car: function(e) {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '亲，您确定要删除这条订单吗？',
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.hostname + ":" + app.globalData.port + '/product/delete_car',
            header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            data: {
              car_id: e.currentTarget.id,
            },
            success: function(res) {
              that.login()
            }
          })
        } else if (res.cancel) {

        }
      }
    })
  },

  // 立即预定
  toBuy() {
    let that = this;
    var data_list = [];
    this.setData({
      showDialog: !this.data.showDialog,
      selectAllStatus: false,
      totalPrice: 0,
      totalMoney: 0
    });
    var carts = that.data.carts
    for (var i = 0; i < carts.length; i++) {
      if (carts[i].isSelect == true) {
        data_list.push({
          id: carts[i].id,
          name: carts[i].name,
          count: carts[i].num,
          product_id: carts[i].product_id,
          price: carts[i].price
        })
      }
    }
    // wx.request({
    //   url: app.globalData.hostname + ":" + app.globalData.port + '/car_list/add_cart',
    //   header: {
    //     'content-type': 'application/x-www-form-urlencoded' // 默认值
    //   },
    //   data: {
    //     data_list: data_list,
    //   },
    //   success: function (res) {
    //   }
    // })
    // 跳转到form表单页面
    if (data_list.length == 0) {
      wx.showModal({
        title: '提示',
        content: '亲，您还没有选定车辆哦~~',
        success: function(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: "/pages/cart_list/cart_list"
            });
          } else if (res.cancel) {
            wx.navigateTo({
              url: "/pages/cart_list/cart_list"
            });
          }
        }
      })
    } else {
      app.globalData.car_list = data_list,
        wx.navigateTo({
          url: "/pages/reserve_car/reserve_car"
        });
    }
  },
  //数量变化处理
  handleQuantityChange(e) {
    var componentId = e.componentId;
    var quantity = e.quantity;
    this.data.carts[componentId].count.quantity = quantity;
    this.setData({
      carts: this.data.carts,
    });
  },
  login: function() {
    let that = this;
    var id_list = [];
    let token = wx.getStorageSync('token');
    var wxid = app.globalData.wxid
    //获取服务热线和联系我们电话
    if (wxid !== null) {
      wx.request({
        url: app.globalData.hostname + ":" + app.globalData.port + '/product/car_rental_list',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        data: {
          user_id: app.globalData.wxid,
        },
        success: function(res) {
          if (res.data.carts) {
            for (var i = 0; i < res.data.carts.length; i++) {
              var array = wx.base64ToArrayBuffer(res.data.carts[i].pic);
              var base64 = wx.arrayBufferToBase64(array);
              res.data.carts[i].pic = "data:image/png;base64," + base64.substr(1);
            }
            that.setData({
              carts: res.data.carts
            })
          }
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '亲，您还没有登录哦~~',
        success: function (res) {
          if (res.confirm) {
            wx.switchTab({
              url: "/pages/user/index"
            });
          } else if (res.cancel) {
            wx.switchTab({
              url: '/pages/user/index'
            })
          }
        }
      })
      
    }
  },
  onShow: function() {
    var that = this
    that.login()
  },
});